// pages/board/board.js
import {
  hgetStorage,
  hnavigateTo,
  hshowToast
} from "../../static/htools.wx";
import {
  hrequestGet
} from "../../static/hunit.wx";
import api from '../../assets/api'
const {
  DeclareYear,
  DeclareMonth,
  getDeclareDay
} = api
import {
  hMouthDay,
  hyearMouth,
  hmouth,
  hyearMouthDay
} from '../../static/handleTimes'
Page({
  data: {
    currentTab: 0,
    curMouth: '',
    yearData: [],
    mouthData: [],
    dayData: [],
    dayDataLen: 0,
    dayPage: 0,
    daySize: 3,
    isShowDayMore: true,
    dayTimer: null,
    dayTotalSize: 0,
    mouthPage: 0,
    mouthSize: 2,
    isShowMouthMore: true,
    mouthTotalSize: 0,
    mouthTimer: null,
    yearPage: 0,
    yearSize: 2,
    isShowYearMore: true,
    yearTotalSize: 0,
    yearTimer: null,
    isYearUpdate: false,
    isMouthUpdate: false,
    isDayUpdate: false,
    yearCountDown: ''
  },
  onLoad() {
    this.getDeclareDayList()
  },
  onShow() {
    if (this.data.isYearUpdate) {
      this.setData({
        yearPage: 0
      })
      this.getYearData();
    }
    if (this.data.isMouthUpdate) {
      this.setData({
        mouthPage: 0
      })
      this.getMouthData();
    }
    if (this.data.isDayUpdate) {
      this.setData({
        dayPage: 0
      })
      this.getDeclareDayList();
    }
  },
  // 点击去申报 去修改
  updateDetail(e) {
    let _id = e.currentTarget.dataset.id
    let route = e.currentTarget.dataset.route
    hnavigateTo('/pages/' + route + '/' + route + '?id=' + _id)
  },
  // 获取日前列表
  async getDeclareDayList() {
    let _dataJson = {
      deptIds: hgetStorage('deptId'),
      page: this.data.dayPage,
      size: this.data.daySize
    }
    let _res = await hrequestGet(getDeclareDay, _dataJson);
    if (_res.statusCode == 400) {
      hshowToast("没有权限")
      this.setData({
        isShowDayMore: false
      })
    }
    if (_res.data.content.length == 0 || _res.data.content.length < this.data.daySize || _res.data.content.length == _res.data.totalElements) {
      this.setData({
        isShowDayMore: false
      })
    }
    if (_res.data.content.length > 0) {
      _res.data.content.forEach(item => {
        item.mouthDay = hMouthDay(item.declareDay)
        item.yearMouthDay = hyearMouthDay(item.createTime)
      });
      if (this.data.dayPage == 0) {
        this.setData({
          dayData: _res.data.content,
          dayTotalSize: _res.data.totalElements
        })
      } else {
        this.setData({
          dayData: this.data.dayData.concat(_res.data.content),
          dayTotalSize: _res.data.totalElements
        })
      }
      if (this.data.dayData.length == this.data.dayTotalSize) {
        this.setData({
          isShowDayMore: false
        })
      }
      this.countDownDay()
    }
  },
  //点击日前查看更多
  getMoreDay() {
    let _page = this.data.dayPage
    this.setData({
      dayPage: ++_page,
      daySize: 3,
    })
    this.getDeclareDayList()
  },
  // 请求 月度数据
  async getMouthData() {
    let _dataJson = {
      deptIds: hgetStorage('deptId'),
      page: this.data.mouthPage,
      size: this.data.mouthSize
    }
    let _res = await hrequestGet(DeclareMonth, _dataJson);
    if (_res.statusCode == 400) {
      hshowToast("没有权限")
      this.setData({
        isShowMouthMore: false
      })
    }
    if (_res.data.content.length == 0 || _res.data.content.length < this.data.mouthSize || _res.data.content.length == _res.data.totalElements) {
      this.setData({
        isShowMouthMore: false
      })
    }
    if (_res.data.content.length > 0) {
      _res.data.content.forEach(item => {
        item.curYearMouth = hyearMouth(item.declareMonth)
        item.curMouth = hmouth(item.declareMonth)
      });
      if (this.data.mouthPage == 0) {
        this.setData({
          mouthData: _res.data.content,
          mouthTotalSize: _res.data.totalElements
        })
      } else {
        this.setData({
          mouthData: this.data.mouthData.concat(_res.data.content),
          mouthTotalSize: _res.data.totalElements
        })
      }
      this.countDownMouth()
    }
  },
  //点击月度查看更多
  getMoreMouth() {
    let _page = this.data.mouthPage
    this.setData({
      mouthPage: ++_page,
      mouthSize: 2
    })
    this.getMouthData()
  },
  // 请求年度数据
  async getYearData() {
    let _dataJson = {
      deptIds: hgetStorage('deptId'),
      page: this.data.yearPage,
      size: 2
    }
    let _res = await hrequestGet(DeclareYear, _dataJson);
    if (_res.statusCode == 400) {
      hshowToast("没有权限")
      this.setData({
        isShowYearMore: false
      })
    }
    if (_res.data.content.length == 0 || _res.data.content.length < this.data.yearSize || _res.data.content.length == _res.data.totalElements) {
      this.setData({
        isShowYearMore: false
      })
    }
    if (_res.data.content.length > 0) {
      if (this.data.yearPage == 0) {
        this.setData({
          yearData: _res.data.content,
          yearTotalSize: _res.data.totalElements
        })
      } else {

        this.setData({
          yearData: this.data.yearData.concat(_res.data.content),
          yearTotalSize: _res.data.totalElements
        })
      }
      this.countDownYear()
    }
  },
  //点击年度查看更多
  getMoreYear() {
    let _page = this.data.yearPage
    this.setData({
      yearPage: ++_page,
      yearSize: 2,
    })
    this.getYearData()
  },
  //滑动切换
  swiperTab(e) {
    this.setData({
      currentTab: e.detail.current
    });
    if (this.data.currentTab == 0) {
      this.setData({
        dayPage: 0,
        isShowDayMore: true
      })
      this.getDeclareDayList();
    } else if (this.data.currentTab == 1) {
      this.setData({
        mouthPage: 0,
        isShowMouthMore: true
      })
      this.getMouthData();
    } else if (this.data.currentTab == 2) {
      this.setData({
        yearPage: 0,
        isShowYearMore: true
      })
      this.getYearData();
    }
  },
  //点击切换
  clickTab(e) {
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      this.setData({
        currentTab: e.target.dataset.current
      })
    }
    if (this.data.currentTab == 0) {
      this.setData({
        dayPage: 0,
        isShowDayMore: true
      })
      this.getDeclareDayList();
    } else if (this.data.currentTab == 1) {
      this.setData({
        mouthPage: 0,
        isShowMouthMore: true
      })
      this.getMouthData();
    } else if (this.data.currentTab == 2) {
      this.setData({
        yearPage: 0,
        isShowYearMore: true
      })
      this.getYearData();
    }
  },
  //时间格式的转换
  timeFormat(allTime) {
    let h = Math.floor(allTime / 3600) >= 10 ? Math.floor(allTime / 3600) : '0' + Math.floor(allTime / 3600);
    let _ss = allTime - h * 3600;
    let m = Math.floor(_ss / 60) >= 10 ? Math.floor(_ss / 60) : '0' + Math.floor(_ss / 60);
    let s = _ss - m * 60 >= 10 ? _ss - m * 60 : '0' + _ss - m * 60;
    return h + ":" + m + ":" + s;
  },
  //倒计时日前
  countDownDay() {
    clearInterval(this.data.dayTimer)
    let _data = this.data.dayData
    if (_data.length > 0) {
      let _timer = setInterval(() => {
        _data.forEach(item => {
          item.expire--;
          item.hhmmss = this.timeFormat(item.expire);
        })
        this.setData({
          dayData: _data
        })
      }, 1000)
      this.setData({
        dayTimer: _timer
      })
    }
  },
  /*倒计时月度*/
  countDownMouth() {
    clearInterval(this.data.mouthTimer)
    let _data = this.data.mouthData
    if (_data.length > 0) {
      let _timer = setInterval(() => {
        _data.forEach(item => {
          item.expire--;
          item.hhmmss = this.timeFormat(item.expire)
        })
        this.setData({
          mouthData: _data
        })
      }, 1000)
      this.setData({
        mouthTimer: _timer
      })
    }
  },
  /*倒计时年度*/
  countDownYear() {
    clearInterval(this.data.yearTimer)
    let _data = this.data.yearData
    if (_data.length > 0) {
      let _timer = setInterval(() => {
        _data.forEach(item => {
          item.expire--
          item.hhmmss = this.timeFormat(item.expire)
        })
        this.setData({
          yearData: _data
        })
      }, 1000)
      this.setData({
        yearTimer: _timer
      })
    }
  }
})